<?php

session_start();

require_once("includes/connect.php");
require_once("includes/function.php");

$panngan2 = $_REQUEST["panngan2"];
$panngan3 = $_REQUEST["panngan3"];
$date_create = $_REQUEST["date_create"];
$comment = $_REQUEST["comment"];

$month_year = $_REQUEST["month_year"];
$budget_year = $_REQUEST["budget_year"];

$ex1_id = $_REQUEST["ex1_id"];
$ex2_id = $_REQUEST["ex2_id"];
$ex3_id = $_REQUEST["ex3_id"];

$val = $_POST["val"];

$array1 = $_POST["array1"];

//$array2 = $_POST["array2"];

$pet_id = $_REQUEST["pet_id"];

$task = $_REQUEST["task"];

$cancel_date = $_REQUEST["cancel_date"];
$cancel_reason = $_REQUEST["cancel_reason"];

$id = $_REQUEST["id"];
//echo "task = $task, ex3_id = $ex3_id";
switch ($task) {
    case "new":
        $db = NEW myDBs();
        $running = $db->get_running($budget_year);

        for ($i = 0; $i < count($val); $i++) {
            $str_in = $str_in . "'" . $val[$i] . "',";
        }
        $str_in = $str_in . "''";

        $sql = "SELECT * FROM tbl_init
                WHERE  card_id IN ($str_in)
                AND panngan2_id = '$panngan2'
                AND panngan3_id = '$panngan3'
                AND month_year = '$month_year'
                AND budget_year = '$budget_year'
                ";

        //echo $sql;
        //$db1->selectDB("account");
        $result = $db->runSQL($sql);

        $db1 = NEW myDBS;
        $db1->selectDB("account");

        $db2 = NEW myDBS;
        $db2->selectDB("account");


        while ($rows = $db->fetch_object($result)) {

            switch ($ex3_id) {
                case "33":
                    $sql = "select (tax+cremation1+welfare+cooperative+loan+bongkum+ghb+aia+cremation2+other2) as total_all FROM tbl_init where card_id = '$rows->card_id' and month_year = '$month_year'";

                    $result2 = $db2->runSQL($sql);
                    $rows2 = $db2->fetch_object($result2);

                    //echo $rows2->total_all;


                    $sql = "INSERT INTO tbl_salary 
                            (id, lvl1_id, panngan2_id, panngan3_id, ex1_id, ex2_id, ex3_id, month_year, budget_year, salary, pet_id, exp_total, date_create, comment)
                            VALUES('$rows->card_id', '" . $_SESSION["lvl1_id"] . "', '$panngan2', '$panngan3', '$ex1_id', '$ex2_id', '$ex3_id', '$month_year', '$budget_year', '$rows->salary', '$running', '$rows2->total_all', '$date_create', '$comment')
                            ";
                    
//                    echo "adsfadsfasdf";
                echo $sql;

                   $db1->runSQL($sql) or die(mysql_error());

                    break;
                case "34":
                    $sql = "INSERT INTO tbl_salary 
                        (id, lvl1_id, panngan2_id, panngan3_id, ex1_id, ex2_id, ex3_id, month_year, budget_year, salary, pet_id, date_create, comment)
                        VALUES('$rows->card_id', '" . $_SESSION["lvl1_id"] . "', '$panngan2', '$panngan3', '$ex1_id', '$ex2_id', '$ex3_id', '$month_year', '$budget_year', '$rows->extra', '$running', '$date_create', '$comment')
                        ";
                    //echo $sql;

                    $db1->runSQL($sql);

                    break;
                case "35":

                    $sql = "INSERT INTO tbl_salary 
                    (id, lvl1_id, panngan2_id, panngan3_id, ex1_id, ex2_id, ex3_id, month_year, budget_year, salary, pet_id, date_create, comment)
                    VALUES('$rows->card_id', '" . $_SESSION["lvl1_id"] . "', '$panngan2', '$panngan3', '$ex1_id', '$ex2_id', '$ex3_id', '$month_year', '$budget_year', '$rows->emoluments', '$running', '$date_create', '$comment')
                    ";
                    //echo $sql;

                    $db1->runSQL($sql);
                    break;
                default :

                    break;
            }
        }
        switch ($ex3_id) {
            case "33":
                $status = ",salary";
                break;
            case "34":
                $status = ",extra";

                break;
            case "35":
                $status = ",emoluments";
                //$amount1 = $rows->emoluments;
                break;
            default :
                $status = "";
                //$amount1 = -1;
                break;
        }
        $sql = "UPDATE tbl_init SET status = concat(status,'$status')
                WHERE  card_id IN ($str_in)
                AND panngan2_id = '$panngan2'
                AND panngan3_id = '$panngan3'
                AND month_year = '$month_year'
                AND budget_year = '$budget_year'
                ";
        $db1->runSQL($sql);
        //echo $running;

        break;
    case "edit":
        $db = NEW myDBS();
        $db->selectDB("account");
        $sql = "UPDATE tbl_salary 
                SET panngan2_id = '$panngan2',
                panngan3_id = '$panngan3',
                ex1_id = '$ex1_id',
                ex2_id = '$ex2_id',
                ex3_id = '$ex3_id',
                month_year = '$month_year',
                budget_year = '$budget_year'
                WHERE pet_id = '$pet_id'
            ";
        //echo $array2[1];
        //echo $sql;
        //$db->runSQL($sql);


        $sql = "select id from tbl_salary where pet_id = '$pet_id' order by id";
        $result = $db->runSQL($sql);

        $db1 = NEW myDBS;
        $db1->selectDB("account");
        $i = 0;
        while ($rows = $db->fetch_object($result)) {
            $sql = "UPDATE tbl_salary SET salary = '" . $array1[$i] . "'
                    WHERE id = '" . $rows->id . "' and pet_id = '$pet_id'";
            
            //echo $sql ."<br>";
            $db1->runSQL($sql);
            //echo "array2[$i] = ".$array2[$i]."<br>";
            $i++;
            /*
              $sql = "UPDATE tbl_init SET "

              switch ($ex3_id) {
              case "23":
              $sql = $sql ."rent_home = '". $array1[$i] ."'";
              break;
              case "24":
              $sql = $sql ."education = '". $array1[$i] ."'";
              break;
              case "25":
              $sql = $sql ."medical_fee = '". $array1[$i] ."'";
              break;
              }

              $sql = $sql. " WHERE id = '". $rows->id ."' and month_year = '$month_year'";
              $db1->runSQL($sql);
              $i++;
             */
        }

        break;
    case "del":
        $db = NEW myDBS();

        $sql = "DELETE FROM tbl_salary WHERE pet_id = '$pet_id'";
        $db->runSQL($sql);
        //echo $sql;
        break;
    
    case "cancel":
        
        $db = NEW myDBS();
        $sql = "UPDATE tbl_salary "
                . " SET cancel_date = '$cancel_date', "
                . " cancel_reason = '$cancel_reason', "
                . " status = 'ยกเลิก' "
                . " WHERE pet_id = '$id'";
        //echo $sql;

        $db->runSQL($sql) or die(mysql_error());
        
        break;
                
}
?>
